package com.gaggle.dao;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

public abstract class BaseDaoImpl<T> implements BaseDao<T> {

	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	@Override
	public T create(T entity) {
		hibernateTemplate.save(entity);
		hibernateTemplate.flush();
		hibernateTemplate.refresh(entity);
		return entity;
	}

	@Override
	public T saveOrUpdate(T entity) {
		hibernateTemplate.saveOrUpdate(entity);
		return entity;
	}

	@Override
	public void delete(T entity) {
		hibernateTemplate.delete(entity);
	}

}
